iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0

今天新增的功能是在輸入的時候,若不小心輸入錯誤的話,可以修改輸入的值,不用再重新跑一次程式。
先新增一個boolean值,用來控制while迴圈,迴圈內容是請使用者輸入資料,下面有詢問使用者要不要修改資料,如果輸入的是"no"以外的話,那就會重新輸入一次,並修改成新輸入的值。

 boolean continueInserting = true;

        while(continueInserting) {
        System.out.println("請輸入學號:");
        int number = sc.nextInt();
        sc.nextLine();
        System.out.println("請輸入姓名:");
        String name = sc.nextLine();
        System.out.println("請輸入性別:");
        String gender = sc.nextLine();
        System.out.println("請輸入年齡:");
        int age = sc.nextInt();
        sc.nextLine();

        System.out.println("Do you want to modify data? (yes/no)");
        String userInput = sc.nextLine();
            if (userInput.equalsIgnoreCase("no")) {
                continueInserting = false;
                insertData(number, name, gender, age, sc);
            }
        }

來看看實際的操作結果吧~~
我們輸入一筆資料(number, name, gender, age)<-(16, 小明, 男, 22)

https://ithelp.ithome.com.tw/upload/images/20241001/20169406AyJMKIOEux.png
可以看到,我不小心打成"小鳴"了,所以問我要不要修改的時候,我就回答是,之後輸入正確的"小明",再確定提交就完成了。
https://ithelp.ithome.com.tw/upload/images/20241001/20169406Mwxa959c5x.png
提交的資料確實是我們修改後的"小明"哦~~


上一篇
D16:學習Commit處理(下)
下一篇
D18:資料表格輸出
系列文
資料庫與Java開發工具連接26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言